<?php
namespace app\mobile\controller;

ini_set('date.timezone','Asia/Shanghai');
use think\Db;
use think\Loader;
use think\Request;
use think\Session;
use think\Url;
use think\Cache;

/**
 * Class Wxpay
 * @package app\mobile\controller   微信支付 手机站
 */
class Wxpay extends BaseController
{
    /*
     * 支付
     */
    public function pay($attach,$total_price,$out_trade_no)
    {

        if (!$out_trade_no){  //支付完毕选择返回时$out_trade_no为null
            $this->redirect(Url::build('mobile/user_info/my_shop_index'));
        }
        header("Content-type:text/html;charset=utf-8");
        Loader::import('wxpay.mobile.lib.WxPay',EXTEND_PATH,'.Config.php');  //配置文件
        include_once EXTEND_PATH.'wxpay/mobile/lib/WxPay.Api.php';
        include_once EXTEND_PATH.'wxpay/mobile/lib/WxPay.Data.php';
        Loader::import('wxpay.mobile.example.WxPay',EXTEND_PATH,'.JsApiPay.php');
        Loader::import('wxpay.mobile.example.log',EXTEND_PATH);

        //①、获取用户openid
        $tools = new \JsApiPay();

        $member_id  =Session::get('member_id');
        if (empty(Cache::has('open_id'.$member_id))) {
            $openId = $tools->GetOpenid();

            Cache::set('open_id'.$member_id,$openId,1800);
        }else{
            $openId = Cache::get('open_id'.$member_id);
        }

        $total_price = bcmul($total_price,100);

        //②、统一下单
        $input = new \WxPayUnifiedOrder();
        $input->SetBody("Ishop微信支付");
        $input->SetAttach($attach);
        $input->SetOut_trade_no($out_trade_no);
        $input->SetTotal_fee($total_price);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 600));
        $input->SetGoods_tag("Ishop");   //商品标记，优惠券时可能用到;
        $input->SetNotify_url(Request::instance()->domain() . Url::build('mobile/WxAction/notifyurl'));
        $input->SetTrade_type("JSAPI");
        $input->SetOpenid($openId);
        $order = \WxPayApi::unifiedOrder($input);
        $jsApiParameters = $tools->GetJsApiParameters($order);

        return $jsApiParameters;
    }
    /*
     * 充值
     */
    public function recharge($out_trade_no, $total_price)
    {

        header("Content-type:text/html;charset=utf-8");
        Loader::import('wxpay.mobile.lib.WxPay',EXTEND_PATH,'.Config.php');  //配置文件
        include_once EXTEND_PATH.'wxpay/mobile/lib/WxPay.Api.php';
        include_once EXTEND_PATH.'wxpay/mobile/lib/WxPay.Data.php';
        Loader::import('wxpay.mobile.example.WxPay',EXTEND_PATH,'.JsApiPay.php');
        Loader::import('wxpay.mobile.example.log',EXTEND_PATH);

        //①、获取用户openid
        $tools = new \JsApiPay();
        $member_id  =Session::get('member_id');

        if (empty(Cache::has('open_id'.$member_id))) {
            $openId = $tools->GetOpenid();
            Cache::set('open_id'.$member_id,$openId,1800);
        }else{
            $openId = Cache::get('open_id'.$member_id);
        }
        // $openId = $tools->GetOpenid();
        $total_price = bcmul((float)$total_price,100);
        //②、统一下单
        $input = new \WxPayUnifiedOrder();
        $input->SetBody("Ishop微信支付");
        $input->SetAttach("recharge|".Session::get('member_id'));
        $input->SetOut_trade_no($out_trade_no);
        $input->SetTotal_fee($total_price);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 600));
        $input->SetGoods_tag("Ishop");   //商品标记，优惠券时可能用到;
        $input->SetNotify_url(Request::instance()->domain() . Url::build('mobile/WxAction/notifyurl'));
        $input->SetTrade_type("JSAPI");
        $input->SetOpenid($openId);

        $order = \WxPayApi::unifiedOrder($input);

        $jsApiParameters = $tools->GetJsApiParameters($order);

        return $jsApiParameters;

    }


    /*
     * 成为VIP
     */
    public function check_vip($out_trade_no, $total_price)
    {

        header("Content-type:text/html;charset=utf-8");
        Loader::import('wxpay.mobile.lib.WxPay',EXTEND_PATH,'.Config.php');  //配置文件
        include_once EXTEND_PATH.'wxpay/mobile/lib/WxPay.Api.php';
        include_once EXTEND_PATH.'wxpay/mobile/lib/WxPay.Data.php';
        Loader::import('wxpay.mobile.example.WxPay',EXTEND_PATH,'.JsApiPay.php');
        Loader::import('wxpay.mobile.example.log',EXTEND_PATH);

        //①、获取用户openid
        $tools = new \JsApiPay();
        $member_id  =Session::get('member_id');
        if (empty(Cache::has('open_id'.$member_id))) {
            $openId = $tools->GetOpenid();
            Cache::set('open_id'.$member_id,$openId,1800);
        }else{
            $openId = Cache::get('open_id'.$member_id);
        }
        $total_price = bcmul((float)$total_price,100);
        //②、统一下单
        $input = new \WxPayUnifiedOrder();
        $input->SetBody("Ishop微信支付");
        $input->SetAttach("vip|".Session::get('member_id'));
        $input->SetOut_trade_no($out_trade_no);
        $input->SetTotal_fee($total_price);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 600));
        $input->SetGoods_tag("Ishop");   //商品标记，优惠券时可能用到;
        $input->SetNotify_url(Request::instance()->domain() . Url::build('mobile/WxAction/notifyurl'));
        $input->SetTrade_type("JSAPI");
        $input->SetOpenid($openId);
        $order = \WxPayApi::unifiedOrder($input);
        $jsApiParameters = $tools->GetJsApiParameters($order);
        return $jsApiParameters;
    }


    /*
     * 拍卖支付
     */
    public function auction($total_price,$out_trade_no)
    {
        header("Content-type:text/html;charset=utf-8");
        Loader::import('wxpay.mobile.lib.WxPay',EXTEND_PATH,'.Config.php');  //配置文件
        include_once EXTEND_PATH.'wxpay/mobile/lib/WxPay.Api.php';
        include_once EXTEND_PATH.'wxpay/mobile/lib/WxPay.Data.php';
        Loader::import('wxpay.mobile.example.WxPay',EXTEND_PATH,'.JsApiPay.php');
        Loader::import('wxpay.mobile.example.log',EXTEND_PATH);


        //①、获取用户openid
        $tools = new \JsApiPay();

        $member_id  =Session::get('member_id');
        if (empty(Cache::has('open_id'.$member_id))) {
            $openId = $tools->GetOpenid();
            Cache::set('open_id'.$member_id,$openId,1800);
        }else{
            $openId = Cache::get('open_id'.$member_id);
        }

        $total_price = bcmul((float)$total_price,100);
        //②、统一下单
        $input = new \WxPayUnifiedOrder();
        $input->SetBody("喜利在线微信支付");
        $input->SetAttach("auction|".Session::get('member_id')."|".$out_trade_no);
        $input->SetOut_trade_no($out_trade_no);
        $input->SetTotal_fee($total_price);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 600));
        $input->SetGoods_tag("喜利在线");   //商品标记，优惠券时可能用到;
        $input->SetNotify_url(Request::instance()->domain() . Url::build('mobile/WxAction/notifyurl'));
        $input->SetTrade_type("JSAPI");
        $input->SetOpenid($openId);
        $order = \WxPayApi::unifiedOrder($input);

        $jsApiParameters = $tools->GetJsApiParameters($order);

        return $jsApiParameters;
    }

    /**
     *
     */
    public function pays($table, $id)
    {
        $ret = Db::name($table)->where('id', $id)->delete();
        if ($ret) {
            return ['code' => 200];
        }

    }
}